package cn.wolfcode.shop.web.listener;

import cn.wolfcode.shop.redis.RedisService;
import cn.wolfcode.shop.redis.SeckillGoodPrefixKey;
import cn.wolfcode.shop.service.ISeckillGoodService;
import cn.wolfcode.shop.vo.GoodVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by wolfcode-lanxw
 */
@Component
public class InitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private RedisService redisService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        List<GoodVo> goodVoList = seckillGoodService.queryCache();
        for(GoodVo goodVo:goodVoList){
            redisService.setNx(SeckillGoodPrefixKey.SECKILL_STOCK,goodVo.getId()+"",goodVo.getStockCount());
        }
    }
}
